<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>输出字符串</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.parse-str.html">« parse_str</a></li>
      <li style="float: right;"><a href="function.printf.html">printf »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.strings.html">字符串 函数</a></li>
    <li>输出字符串</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.print" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">print</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">print</span> &mdash; <span class="dc-title">输出字符串</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.print-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>print</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$expression</code></span>): <span class="type">int</span></div>

  <p class="para rdfs-comment">
   输出 <code class="parameter">expression</code>。
  </p>
  <p class="para">
   <code class="literal">print</code> 不是函数而是语言结构。它的参数是跟在 <code class="literal">print</code>
   关键字后面的表达式，并且不用括号分割。
  </p>
  <p class="para">
   和 <span class="function"><a href="function.echo.html" class="function">echo</a></span> 最主要的区别是 <code class="literal">print</code>
   仅接受一个参数，并始终返回 <code class="literal">1</code>。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.print-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">expression</code></dt>

     <dd>

      <p class="para">
       要输出的表达式。即使启用 <a href="language.types.declarations.html#language.types.declarations.strict" class="link"><code class="literal">strict_types</code>
       指令</a>，非字符串也会强制转换为字符串。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.print-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   总是返回 <code class="literal">1</code>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.print-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4796">
    <p><strong>示例 #1 <span class="function"><strong>print</strong></span> 范例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #DD0000">"print&nbsp;does&nbsp;not&nbsp;require&nbsp;parentheses."</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;不会新增新行或者空格；下面会在一行中输出“helloworld”<br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #DD0000">"hello"</span><span style="color: #007700">;<br />print&nbsp;</span><span style="color: #DD0000">"world"</span><span style="color: #007700">;<br /><br />print&nbsp;</span><span style="color: #DD0000">"This&nbsp;string&nbsp;spans<br />multiple&nbsp;lines.&nbsp;The&nbsp;newlines&nbsp;will&nbsp;be<br />output&nbsp;as&nbsp;well"</span><span style="color: #007700">;<br /><br />print&nbsp;</span><span style="color: #DD0000">"This&nbsp;string&nbsp;spans\nmultiple&nbsp;lines.&nbsp;The&nbsp;newlines&nbsp;will&nbsp;be\noutput&nbsp;as&nbsp;well."</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;参数可以是任何生成字符串的表达式<br /></span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"example"</span><span style="color: #007700">;<br />print&nbsp;</span><span style="color: #DD0000">"foo&nbsp;is&nbsp;</span><span style="color: #0000BB">$foo</span><span style="color: #DD0000">"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;foo&nbsp;is&nbsp;example<br /><br /></span><span style="color: #0000BB">$fruits&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #DD0000">"lemon"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"orange"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"banana"</span><span style="color: #007700">];<br />print&nbsp;</span><span style="color: #0000BB">implode</span><span style="color: #007700">(</span><span style="color: #DD0000">"&nbsp;and&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$fruits</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;lemon&nbsp;and&nbsp;orange&nbsp;and&nbsp;banana<br /><br />//&nbsp;即使使用了&nbsp;declare(strict_types=1)，非字符串表达式也会强制转换为字符串<br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #0000BB">6&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">7</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;42<br /><br />//&nbsp;因为&nbsp;print&nbsp;有返回值，所以可以在如下表达式中使用<br />//&nbsp;以下输出“hello&nbsp;world”<br /></span><span style="color: #007700">if&nbsp;(&nbsp;print&nbsp;</span><span style="color: #DD0000">"hello"&nbsp;</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&nbsp;world"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;以下输出“true”<br /></span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">)&nbsp;?&nbsp;print&nbsp;</span><span style="color: #DD0000">'true'&nbsp;</span><span style="color: #007700">:&nbsp;print&nbsp;</span><span style="color: #DD0000">'false'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.print-notes">
  <h3 class="title">注释</h3>

  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <strong>使用括号</strong><br />
   <p class="para">
    用括号括住 <code class="literal">print</code>
    的参数并不会引发语法错误，而且会产生看起来像是普通函数调用的语法。然而，这可能会产生误导，因为括号实际上是输出表达式的一部分，而非
    <code class="literal">print</code> 语法本身的一部分。
   </p>
   <p class="para">
    <div class="example" id="example-4797">
     
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #DD0000">"hello"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">//&nbsp;输出“hello”<br /><br /></span><span style="color: #007700">print(</span><span style="color: #DD0000">"hello"</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;也会输出“hello”，因为&nbsp;("hello")&nbsp;是有效的表达式<br /><br /></span><span style="color: #007700">print(</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">)&nbsp;*&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">//&nbsp;输出“9”；会首先对括号内的&nbsp;1+2&nbsp;进行求值，然后是&nbsp;3*3<br />//&nbsp;print&nbsp;语句会将整个表达式视为一个参数<br /><br /></span><span style="color: #007700">if&nbsp;(&nbsp;print(</span><span style="color: #DD0000">"hello"</span><span style="color: #007700">)&nbsp;&amp;&amp;&nbsp;</span><span style="color: #0000BB">false&nbsp;</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;</span><span style="color: #DD0000">"&nbsp;-&nbsp;inside&nbsp;if"</span><span style="color: #007700">;<br />}<br />else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;</span><span style="color: #DD0000">"&nbsp;-&nbsp;inside&nbsp;else"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #FF8000">//&nbsp;输出“&nbsp;-&nbsp;inside&nbsp;if”<br />//&nbsp;首先对表达式&nbsp;("hello")&nbsp;&amp;&amp;&nbsp;false&nbsp;求值，&nbsp;false<br />//&nbsp;强制转换为空字符串“”且打印&nbsp;print&nbsp;<br />//&nbsp;结构，然后返回&nbsp;1，所以运行&nbsp;if&nbsp;块中代码<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>

   <p class="para">
    当在大表达式中使用 <code class="literal">print</code> 时，需要将关键字及其参数放在括号中以便得出预期的结果：
   </p>

   <p class="para">
    <div class="example" id="example-4798">
     
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">if&nbsp;(&nbsp;(print&nbsp;</span><span style="color: #DD0000">"hello"</span><span style="color: #007700">)&nbsp;&amp;&amp;&nbsp;</span><span style="color: #0000BB">false&nbsp;</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;</span><span style="color: #DD0000">"&nbsp;-&nbsp;inside&nbsp;if"</span><span style="color: #007700">;<br />}<br />else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;</span><span style="color: #DD0000">"&nbsp;-&nbsp;inside&nbsp;else"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #FF8000">//&nbsp;输出“hello&nbsp;-&nbsp;inside&nbsp;else”<br />//&nbsp;跟上个示例不同，首先对表达式&nbsp;(print&nbsp;"hello")&nbsp;求值<br />//&nbsp;输出“hello”之后，print&nbsp;返回&nbsp;1<br />//&nbsp;由于&nbsp;1&nbsp;&amp;&amp;&nbsp;false&nbsp;为&nbsp;false，因此运行&nbsp;else&nbsp;块中代码<br /><br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #DD0000">"hello&nbsp;"&nbsp;</span><span style="color: #007700">&amp;&amp;&nbsp;print&nbsp;</span><span style="color: #DD0000">"world"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">//&nbsp;输出“world1”；首先对&nbsp;print&nbsp;"world"&nbsp;求值，<br />//&nbsp;然后表达式&nbsp;"hello&nbsp;"&nbsp;&amp;&amp;&nbsp;1&nbsp;传递给左侧的&nbsp;print<br /><br /></span><span style="color: #007700">(print&nbsp;</span><span style="color: #DD0000">"hello&nbsp;"</span><span style="color: #007700">)&nbsp;&amp;&amp;&nbsp;(print&nbsp;</span><span style="color: #DD0000">"world"</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;输出“hello&nbsp;world”；括号强制&nbsp;print&nbsp;表达式<br />//&nbsp;在&nbsp;&amp;&amp;&nbsp;之前求值<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
  </p></blockquote>

  <blockquote class="note"><p><strong class="note">注意</strong>: <span class="simpara">因为是语言构造器而不是函数，不能被
<a href="functions.variable-functions.html" class="link">可变函数</a> 或者
<a href="functions.arguments.html#functions.named-arguments" class="link">命名参数</a> 调用。
</span>
</p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.print-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.echo.html" class="function" rel="rdfs-seeAlso">echo</a> - 输出一个或多个字符串</span></li>
    <li class="member"><span class="function"><a href="function.printf.html" class="function" rel="rdfs-seeAlso">printf()</a> - 输出格式化字符串</span></li>
    <li class="member"><span class="function"><a href="function.flush.html" class="function" rel="rdfs-seeAlso">flush()</a> - 刷新系统输出缓冲区</span></li>
    <li class="member"><a href="language.types.string.html" class="link">指定字面字符串的方式</a></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>